#include "vigversedevice.h"
#include "Main/GateWay.h"
VigverseDevice::VigverseDevice(QObject *parent) :
    BaseInterface<VigverseDevice>(parent)
{
     qDebug()<<"VigverseDevice open----------------";
    m_pInstance = this;
    version2Protocol = new Version2Protocol(this);
    this->registerProtocol(version2Protocol);

  //  registerCmdHandler(ControlVigverseCmd::FunctionCode,ControlVigverseCmd::staticMetaObject, &VigverseDevice::SynchroSystemCmdHandle);
    registerFuncHandler(ControlPointFunc::FunctionCode, &VigverseDevice::controlVigverseFuncHandle);
}


void VigverseDevice::ControlVigverseCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
  /*s  Q_UNUSED(sharedCmd);
   qDebug()<<"ControlVigverse now";
   Package package;

   QByteArray pointDeviceData;
   QByteArray body;
   quint16 bodylength=0;


   foreach(const quint16 &addr,dataList)
   {
       bodylength++;
     //  SavePointStruct getStruct=dataMap.value(addr);
       pointDeviceData.append(DataTypeConver::intToByteArray(getStruct.pointAddr(),2));
       pointDeviceData.append(DataTypeConver::intToByteArray(getStruct.pointValue(),2));
   }
   bodylength=pointDeviceData.size();
   body.append(DataTypeConver::intToByteArray(bodylength,2));
   body.append(pointDeviceData);
   package.setBody(body);
   package.setFunction(0x09);
   package.setId(0x01);
   pointDeviceData=version2Protocol->generateData(package);
   emit sendDataSignal(source,pointDeviceData);*/

}
void VigverseDevice::controlVigverseFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    qDebug()<<"VigverseDevice::controlPointFuncHandle";

    ControlPointFunc *pointControlFunc = qobject_cast<ControlPointFunc*>(sharedFunc.data());
    QList<ControlPointStruct> controlPointList;
    if(pointControlFunc->sourceDevice() != this->name())
    {
        controlPointList.append(pointControlFunc->sourcePointList());
    }
    controlPointList.append(pointControlFunc->controlPointList());

    if(controlPointList.isEmpty())
    {
        return;
    }

    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }

    for(int i=0; i<controlPointList.size(); i++)
    {
        ControlPointStruct controlPoint = controlPointList.at(i);
        int address = controlPoint.address();
        int value = controlPoint.value();

        printf("address--%d,value---%d\r\n",address,value);

        if(address < 30000)
            return;

        quint64 getDelayTime=controlPoint.delayTime();
        QList <QString>ControlPortlist=controlPoint.Portlist();
        bool RightPort=false;
        foreach(const QString &PostType, ControlPortlist)
        {
            if(PostType=="RS485")
            {
                RightPort=true;
            }
       //     qDebug()<<"PostType="<<PostType;
        }
     //   qDebug()<<"ControlPortlist.size="<<QString::number(controlPoint.Portlist().size());
        if(ControlPortlist.size()==0||RightPort==true)
        {
     //       qDebug()<<"true send data";
           // if(getDelayTime==0)
          //  {
                qDebug()<<"Time is zero now"<<address%1000/10<<" "<<value;
                ControlVigverseCmd *setControlVigverseCmd = new ControlVigverseCmd();

                setControlVigverseCmd->setRoomNO(address/1000-30);
                setControlVigverseCmd->setCtlDevType(address%1000/10);
                if(address%1000/10<ControlVigverseCmd::relay4th)
                    setControlVigverseCmd->setRelay(address%1000/10,address%10,value);
                else if(address%1000/10==ControlVigverseCmd::Computer)
                    setControlVigverseCmd->setComputer(value);
                else if(address%1000/10==ControlVigverseCmd::Porject)
                    setControlVigverseCmd->setProject(address%10,value);
                else if(address%1000/10==ControlVigverseCmd::Curtain)
                    setControlVigverseCmd->setCurtain(address%10,value);
                else if(address%1000/10==ControlVigverseCmd::AirCondition)
                    setControlVigverseCmd->setAirCondition(address%10,value);
                else if(address%1000/10==ControlVigverseCmd::Scence)
                    setControlVigverseCmd->setScence(value);
                else if(address%1000/10==ControlVigverseCmd::Restart)
                    setControlVigverseCmd->setRestart(value);
                QSharedPointer<BaseCmd> sharedSetControlPointCmd(setControlVigverseCmd);

               //  SavePointXml *savePointXml =SavePointXml::getInstance();
                // savePointXml->SavePointData(address,value);
               //  QString stata="Write-RS485";
               //  savePointXml->JoinSystemList(address,value,stata);

                sendCmd(target,sharedSetControlPointCmd);
           // }
        }

    }

}
